package com.sheep.jucdemo.part6.part6_3;

import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Date 2022/4/24 23:34
 * @Created by sheep
 */
public class Test6_3_4_1 {

    // 线程不安全的list
    private ArrayList<String> array = new ArrayList<String>();
    // 独占锁
    private volatile ReentrantLock lock = new ReentrantLock();

    // 添加元素
    public void add(String e){
        lock.lock();
        try {
            array.add(e);
        }finally {
            lock.unlock();
        }
    }
    // 删除元素
    public void remove(String e){
        lock.lock();
        try {
            array.remove(e);
        }finally {
            lock.unlock();
        }
    }
    // 获取数据
    public String get(int index){
        lock.lock();
        try {
            return array.get(index);
        }finally {
            lock.unlock();
        }
    }
}
